//
//import 'dart:convert';
//import 'dart:core';
//
import 'dart:convert';
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/basewidget/HttpUtils/HttpBaseConst.dart';
import 'package:flutter_ahj_app/base/bean/show_files_bean.dart';
import 'package:flutter_ahj_app/base/cunchu/Constant.dart';
import 'package:flutter_ahj_app/base/utils/HttpNoTokenUtils.dart';
import 'package:flutter_ahj_app/base/utils/oss/OSSUrlConsts.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';

import '../HttpUtils.dart';

class ImageWidget extends StatefulWidget {
  final String fileName;

  ///请求接口获得图片的名字
  final String defaultFilesName;

  ///默认显示的文件图标名称
  final double width;

  ///宽
  final double height;

  ///高
  final BoxFit boxFit;

  double borderSize = 3;

  ImageWidget(
      {this.fileName,
      this.width,
      this.height,
      this.defaultFilesName = "默认",
      this.boxFit,
      this.borderSize,
      Key key})
      : super(key: key);

  @override
  _ImageWidgetState createState() => _ImageWidgetState();
}

class _ImageWidgetState extends State<ImageWidget> with WidgetsBindingObserver {
  String _filePath;

  ///网络下载图片的地址

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    ///判断有没filesName 不是https:// 不是本地相册文件
    File file11 = new File(widget.fileName);

    setState(() {
      if (widget.fileName != null) {
        if (widget.fileName.startsWith("http")) {
          _filePath = widget.fileName;
        } else if (widget.fileName.startsWith("public")) {
          _filePath = OSSUrlConsts.URL_UPLOAD_IMAGE_OSS + "/" + widget.fileName;
        } else {
          _filePath = widget.fileName;
        }

        // print("image:" + widget.fileName);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
//      decoration: ShapeDecoration(
//          shape: RoundedRectangleBorder(
//              borderRadius: BorderRadiusDirectional.circular(6))),
      color: Colors.white,
      child: _filePath != null && _filePath.contains("mp4")
          ? Container(
            // padding: EdgeInsets.all(20),
              color: SQColor.lightGray1,
              width: widget.width,
              height: widget.height,
              child: Image.asset(
                "img/yhdj_video.png",
                fit: BoxFit.fill,
                // width: Dimens.dimens20,
                // height: Dimens.dimens20,
              ),
            )
          : _judgeMethod(),
    );
  }

  ///判断是本地还是网络
  Widget _judgeMethod() {
    if (widget.fileName != null) {
      ///判断 onlineName 是不是直接就是图片的URL
      ///https://testxaloss.oss-cn-beijing.aliyuncs.com/1/009dc9a3b3154b44af10900f52aa1ba0.jpg?Expires=1574151432&OSSAccessKeyId=LTAIzpfbqAanWh30&Signature=E%2BbXW%2B1zK383dVLGuvKXqT6nBGw%3D
      if (widget.fileName.startsWith("http")) {
        _filePath = widget.fileName;
      } else {
        File file11 = new File(widget.fileName);
        if (file11.existsSync()) {
          ///区分是本地还是 oss 的文件 名
          return _getImageLocal();
        }
      }
      return _filePath == null ? _getDefaultImage() : _getImageOnLine();
    } else {
      return _getDefaultImage();
    }
  }

  ///加载网络图片
  Widget _getImageOnLine() {
    return CachedNetworkImage(
      imageUrl: _filePath ?? '',
      width: widget.width,
      height: widget.height,
      imageBuilder: (context, imageProvider) => Container(
        width: widget.width,
        height: widget.height,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(widget.borderSize ?? 3),
          image: DecorationImage(
            image: imageProvider,
            fit: widget.boxFit == null ? BoxFit.contain : widget.boxFit,
          ),
        ),
      ),
//      placeholder: (context, url) => Image.asset('img/def_wximage.png'),
      errorWidget: (context, url, error) => Image.asset(''),
    );
  }

  ///加载本地相册图片
  Widget _getImageLocal() {
//    return new Container();
    return Image.file(
      new File(widget.fileName),
      width: widget.width,
      height: widget.height,
      fit: widget.boxFit == null ? BoxFit.contain : widget.boxFit,
    ); //Image.asset(widget.fileName,width: widget.width, height: widget.height,);
  }

  ///默认图片
  Widget _getDefaultImage() {
//    return Container();
    return ClipOval(
      child: Image.asset(
        '',
        width: widget.width,
        height: widget.height,
        fit: BoxFit.fill,
      ),
    );
  }

  ///获得图片路径
  _getImagePath() async {
    var params = {"a": "a"};

    ///fileName  文件名
    /// type  是否是视频1:是 0:不是
    String url = HttpBaseConst.serverUrl +
        HttpBaseConst.downloadPath +
        "?" +
        "fileName=" +
        widget.fileName +
        "&type=0";
    HttpNoTokenUtils.httpGet(url, params, (result) {
      print(result);

      ShowFilesBean showFilesBean = ShowFilesBean.fromJson(json.decode(result));

      print("我是fileUrl" + showFilesBean.data.fileUrl);

      setState(() {
        _filePath = showFilesBean.data.fileUrl;
      });
    }, context: context);
  }
}
